/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateServiceBasicContent extends Content {
	
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;
	
	private VerticalPanel panel = null;
	
	private TextBox name, code;
	private TextArea desc;
	
	public CreateServiceBasicContent(ServiceMessages cons,BifangGWTServiceAsync service,GwtContext context){
		this.cons = cons;
		this.service = service;
		this.context = context;
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");
		
		initWidget(panel);
		
	}
	
	public void onLoad(){
		HTML h = new HTML(cons.fillServiceBasicInfo());
		panel.add(h);
		
		Grid table = new Grid(3,2);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "45%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		

		table.setHTML(0, 0, cons.name());
		table.setHTML(1, 0, cons.code());
		table.setHTML(2, 0, cons.desc());
		
		name = new TextBox();
		name.setVisibleLength(32);

		code = new TextBox();
		code.setVisibleLength(32);

		desc = new TextArea();
		desc.setCharacterWidth(64);
		desc.setVisibleLines(6);
		if(context.get(ServiceConstants.SERVICE) != null){
			Service s = (Service)context.get(ServiceConstants.SERVICE) ;
			name.setText(s.getServiceName());
			code.setText(s.getServiceCode());
			desc.setText(s.getDescription());
			
			
		}
		
		table.setWidget(0, 1, name);
		table.setWidget(1, 1, code);
		table.setWidget(2, 1, desc);
		
		Button b = new Button(cons.next());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				if (!validate())
					return;
				Service s = new Service();
				s.setAppId((String)context.get(ServiceConstants.APP_ID));
				s.setDescription(desc.getText());
				s.setServiceName(name.getText());
				s.setServiceCode(code.getText());
				context.put(ServiceConstants.SERVICE, s);
				
					listener.onContentChanged(
							ServiceContentConstants.CREATE_SERVICE_BASIC,
							ServiceContentConstants.CREATE_SERVICE_CONFIG, null);
				
			}
		});

		panel.add(table);
		panel.add(b);

		
	}
	
	private boolean validate() {
		if (!StringUtils.checkLenght(name.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.name(), "1", "128"));
			return false;
		}
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(code.getText(), 1, 32)) {
			Window.alert(cons.lengthError(cons.code(), "1", "32"));
			return false;

		}
		
		return true;

	}
	

}
